package com.ogawa.project6602.activity

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.github.mikephil.charting.components.Description
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.ValueFormatter
import com.google.android.material.tabs.TabLayout
import com.ogawa.base.utils.ScreenUtils
import com.ogawa.project6602.R
import com.ogawa.project6602.base.Base6602MqttActivity
import com.ogawa.project6602.dialog.HealthIndexEndBottomDialog
import com.ogawa.project6602.dialog.SuanTongReadyDialog
import com.ogawa.project6602.viewmodel.HealthIndexModel
import com.ogawa.project6602.wdiget.HealthIndexMakerView
import com.ogawa.projectcommon.constants.UrlPathConstant
import kotlinx.android.synthetic.main.activity_health_index_first.*
import me.jessyan.autosize.utils.AutoSizeUtils


@Route(path = UrlPathConstant.ACTIVITY_6602_HEALTH_DETECT_INDEX_FIRST)
class healthIndexFirstActivity : Base6602MqttActivity(), View.OnClickListener {
    override fun getLayoutId(): Int {
        return R.layout.activity_health_index_first
    }

    private lateinit var mHealthIndexViewModel: HealthIndexModel
    override fun initView() {
        super.initView()
        setViewModel()
        setUI()
        mHealthIndexViewModel.getReport()
    }

    private fun setViewModel() {
        mHealthIndexViewModel = ViewModelProvider(this).get(HealthIndexModel::class.java)
        mHealthIndexViewModel.getDataEnd.observe(this, Observer {
            setChatLine(
                mHealthIndexViewModel.suantongEntry,
                mHealthIndexViewModel.huoliEntry,
                mHealthIndexViewModel.times
            )
        })
        mHealthIndexViewModel.suantongCheckIng.observe(this, Observer {
            if (it) {
                suantongCheckTv.setText(R.string.checking)
                moreIv.visibility = View.VISIBLE
                Glide.with(mActivity).load(R.drawable.img_wait_gif).into(moreIv);
            } else {
                suantongCheckTv.setText(R.string.checking)
                moreIv.visibility = View.GONE
                Glide.with(mActivity).load(R.drawable.img_wait_gif).into(moreIv)
                var suanTongReadyDialog = HealthIndexEndBottomDialog()
                suanTongReadyDialog.show(supportFragmentManager, "")
            }
        })
    }

    private fun setUI() {
        setTitle(getString(R.string.health_index))
        startSuanTongTv.setOnClickListener(this)
        suantongCarDashboardView.value = 10.0f
        suantongCarDashboardView.setTitle(getString(R.string.suantong_baifenbi))
        suantongCarDashboardView.setDialColor(mActivity.resources.getColor(R.color.color3FC5))
        huoliCarDashboardView.value = 10.0f
        huoliCarDashboardView.setTitle(getString(R.string.huoli_baifenbi))
        huoliCarDashboardView.setDialColor(Color.parseColor("#FF9A88"))

        tablayout.addTab(tablayout.newTab().setText(getString(R.string.day)))
        tablayout.addTab(tablayout.newTab().setText(getString(R.string.month)))
        tablayout.addTab(tablayout.newTab().setText(getString(R.string.year)))
        tablayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            fun setRad(bg: GradientDrawable, position: Int) {
                var rad = ScreenUtils.dip2px(mActivity, 20f).toFloat()
                when (position) {
                    0 -> {
                        bg.cornerRadii = floatArrayOf(rad, rad, 0f, 0f, 0f, 0f, rad, rad)
                    }
                    1 -> {
                        bg.cornerRadii = floatArrayOf(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f)
                    }
                    2 -> {
                        bg.cornerRadii = floatArrayOf(0f, 0f, rad, rad, rad, rad, 0f, 0f)
                    }
                }
                mHealthIndexViewModel.timeFrame = "${position}"
                mHealthIndexViewModel.getReport()
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                var bg = GradientDrawable()
                bg.setColor(mActivity.resources.getColor(R.color.color3FC5))
                tab?.let {
                    setRad(bg, it.position)
                    it.view.background = bg
                }

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                var bg = GradientDrawable()
                bg.setColor(Color.WHITE)
                tab?.let {
                    setRad(bg, it.position)
                    it.view.background = bg
                }
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
                var bg = GradientDrawable()
                bg.setColor(mActivity.resources.getColor(R.color.color3FC5))
                tab?.let {
                    setRad(bg, it.position)
                    it.view.background = bg
                }
            }

        })
        tablayout.selectTab(tablayout.getTabAt(0))
    }

    private fun setChatLine(
        suantongList: ArrayList<Entry>,
        huoliList: ArrayList<Entry>,
        times: ArrayList<String>
    ) {
        for (item in suantongList){
            item.icon = getDrawable(R.drawable.img_item_start)
        }
        for (item in huoliList){
            val drawable = getDrawable(R.drawable.blue_oval_w_bg_orange_stoke)
            drawable.isFilterBitmap = true
            item.icon  = drawable
        }
        var suantongSet = LineDataSet(suantongList, "")
        suantongSet.setColor(Color.parseColor("#66D1FD"))//折线颜色
        suantongSet.setCircleColor(Color.WHITE)//点的颜色
//        suantongSet.fillColor = Color.parseColor("#66D1FD")//填充的颜色
        var suantongbg = GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            intArrayOf(
                Color.parseColor("#49C8FD"),
                Color.parseColor("#3349C8FD")
            )
        )
        suantongSet.fillDrawable =suantongbg
        suantongSet.setLineWidth(1f)//线宽度
        suantongSet.setCircleRadius(3f)//点的大小
        suantongSet.setDrawIcons(true)
        suantongSet.setDrawFilled(true)
        suantongSet.setFormSize(15f)
        suantongSet.setDrawValues(true)//不显示数值
        suantongSet.mode = LineDataSet.Mode.CUBIC_BEZIER//折线为弧度
        suantongSet.valueFormatter = object : ValueFormatter() {
            override fun getPointLabel(entry: Entry?): String? {
                entry?.let {
                    return "" + it.x + "-" + it.y
                }
                return "---"
            }
        }
        var huoliSet = LineDataSet(huoliList, "")
        huoliSet.setColor(Color.parseColor("#99FF9A88"))//折线颜色
        huoliSet.setCircleColor(Color.WHITE)//点的颜色
//        huoliSet.fillColor = Color.parseColor("#FF9A88")//填充的颜色
        var huolibg = GradientDrawable(
            GradientDrawable.Orientation.TOP_BOTTOM,
            intArrayOf(
                Color.parseColor("#FFC8BE"),
                Color.parseColor("#11FFC8BE")
            )
        )
        huoliSet.fillDrawable =huolibg
        huoliSet.setLineWidth(1f)//线宽度
        huoliSet.setCircleRadius(3f)//点的大小
        huoliSet.setDrawIcons(true)
        huoliSet.setDrawFilled(true)
        huoliSet.setFormSize(15f)
        huoliSet.setDrawValues(true)//不显示数值
        huoliSet.mode = LineDataSet.Mode.CUBIC_BEZIER//折线为弧度
        huoliSet.valueFormatter = object : ValueFormatter() {
            override fun getPointLabel(entry: Entry?): String? {
                entry?.let {
                    return "" + it.x + "-" + it.y
                }
                return "---"
            }

        }
        var linearData = LineData()
        linearData.addDataSet(suantongSet)
        linearData.addDataSet(huoliSet)
        linechart.data = linearData
        linechart.axisRight.isEnabled = false
        linechart.axisRight.setDrawGridLines(false)
        linechart.axisRight.removeAllLimitLines()
        linechart.axisLeft.removeAllLimitLines()
        linechart.axisLeft.setSpaceTop(10f)//设置到顶端的距离
        linechart.axisLeft.spaceMin = 10f//设置到顶端的距离
        linechart.axisLeft.axisLineColor = Color.TRANSPARENT
        linechart.axisLeft.gridColor = Color.parseColor("#337C7C7C")
        linechart.xAxis.setDrawLabels(false)
        linechart.xAxis.setDrawGridLines(false)
        linechart.xAxis.removeAllLimitLines()

//        linechart.axisLeft.valueFormatter = object : IndexAxisValueFormatter(xData) {
//            override fun getFormattedValue(value: Float): String {//设置y坐标的文字
//                return "12111"
//            }
//            override fun getFormattedValue(
//                value: Float,
//                entry: Entry?,
//                dataSetIndex: Int,
//                viewPortHandler: ViewPortHandler?
//            ): String {
//                return "12"
//            }
//        }

        linechart.xAxis.position = XAxis.XAxisPosition.BOTTOM
        //去掉图例
        linechart.legend.isEnabled = false
        //去掉描述
        val description: Description = linechart.getDescription()
        description.isEnabled = false
        //去掉十字高亮线
//        linechart.isHighlightPerTapEnabled = false
//        linechart.isHighlightPerDragEnabled = false
        var healthIndexMakerView = HealthIndexMakerView(this)
        healthIndexMakerView.setOffset(
            (-(ScreenUtils.dip2px(mActivity, 25f))).toFloat(),
            (-(ScreenUtils.dip2px(mActivity, 15F))).toFloat()
        )
        linechart.marker = healthIndexMakerView
        linechart.setDrawMarkers(true)

    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.startSuanTongTv -> {
                var suanTongReadyDialog = SuanTongReadyDialog(mActivity)
                suanTongReadyDialog.show()
            }
        }
    }
}